package com.lottery.system.service.activitystatus.operater.impl;

import com.lottery.system.common.errorcode.ServiceErrorCodeConstants;
import com.lottery.system.common.exception.ServiceException;
import com.lottery.system.dao.dataobject.ActivityPrizeDO;
import com.lottery.system.dao.mapper.ActivityPrizeMapper;
import com.lottery.system.dao.mapper.ActivityUserMapper;
import com.lottery.system.service.activitystatus.enums.SequenceEnum;
import com.lottery.system.service.activitystatus.operater.ActivityStatusAbstractOperator;
import com.lottery.system.service.dto.ConvertActivityDTO;
import com.lottery.system.service.enums.ActivityPrizeStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: xiaoxie
 * create: 2024-09-12 20:07
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.service.activitystatus.operater
 * description: 奖品状态扭转策略实现类
 */
@Component
public class PrizeStatusOperatorImpl implements ActivityStatusAbstractOperator {
    private final static Logger logger = LoggerFactory.getLogger(ActivityStatusOperatorImpl.class);

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Autowired
    private ActivityUserMapper activityUserMapper;
    /**
     * @description: 奖品扭转状态责任链排序的策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:52
     * @param: []
     * @return: com.lottery.system.service.activitystatus.enums.SequenceEnum
     **/
    @Override
    public SequenceEnum sequence() {
        return SequenceEnum.BEFORE;
    }

    /**
     * @description: 奖品是否需要扭转状态的策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:10
     * @param: [convertActivityDTO]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean needConvert(ConvertActivityDTO convertActivityDTO) {
       if(null == convertActivityDTO) {
           logger.warn(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR.getMsg());
           throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR);
       }
       //判断活动奖品是否存在
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectActivityPrizeByIdAndActivityId(convertActivityDTO.getActivityId()
                , convertActivityDTO.getPrizeId());
       if(null == activityPrizeDO) {
           logger.warn(ServiceErrorCodeConstants.ACTIVITY_PRIZE_IS_EMPTY.getMsg());
           throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_PRIZE_IS_EMPTY);
       }
        //判断活动状态是否为COMPLETED
        if(activityPrizeDO.getStatus().equalsIgnoreCase(ActivityPrizeStatusEnum.COMPLETED.name())) {
            return false;
        }
       //判断奖品数量是否被全部抽完(有点多余但我感觉多加一行代码更好一点)
        //加一个判断,判断就奖品数量是否等于未读抽取的人员数量
        int count = activityUserMapper.selectCount(convertActivityDTO.getUserIds(),convertActivityDTO.getActivityId());
        if(activityPrizeDO.getPrizeAmount() != count) {
            logger.warn(ServiceErrorCodeConstants.ACTIVITY_PRIZE_OR_USER_IS_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_PRIZE_OR_USER_IS_ERROR);
        }
        return true;
    }

    /**
     * @description: 奖品扭转状态是否成功的策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:11
     * @param: [convertActivityDTO]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean convertStatus(ConvertActivityDTO convertActivityDTO) {
        if(null == convertActivityDTO) {
            logger.warn(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR);
        }
        try {
            activityPrizeMapper.updateStatusByActivityIdAndPrizeId(convertActivityDTO.getActivityId()
                                                                   ,convertActivityDTO.getPrizeId()
                                                                  ,convertActivityDTO.getActivityPrizeStatusTarget().name());
            return true;
        }catch (Exception e) {
            logger.warn("扭转状态失败", e);
            return false;
        }
    }
}
